import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:math';

class GuessPage extends StatefulWidget {
  const GuessPage({super.key});

  @override
  State<GuessPage> createState() => _GuessPage();
}

class _GuessPage extends State<GuessPage> with SingleTickerProviderStateMixin {
  // 随机数
  int _value = 0;
  // 输入的数字
  int inputValue = -1;
  // 是否显示数字
  bool _guessing = false;

  final TextEditingController _controller = TextEditingController();

  void clearTextField() {
    _controller.clear(); // 清空文本
  }

  final Random _random = Random();
  // 随机当前数字
  void _generateRandomValue() {
    setState(() {
      _value = _random.nextInt(100);
      _guessing = false;
      inputValue = -1;
    });
    clearTextField();
  }

  late AnimationController controller;

  @override
  void initState() {
    super.initState();
    _generateRandomValue();
  }

  @override
  Widget build(BuildContext context) {
    String valueStr = '当前随机数：${_guessing ? _value.toString() : '**'}';
    Color bgcBig = inputValue > _value ? Colors.red : Colors.white;
    Color smallBig =
        inputValue != -1 && inputValue < _value ? Colors.green : Colors.white;

    return Scaffold(
      body: Stack(
        children: [
          Column(children: [
            AppBar(
              title: const Text('猜数字'),
              systemOverlayStyle: const SystemUiOverlayStyle(
                statusBarIconBrightness: Brightness.dark,
                statusBarColor: Colors.transparent,
              ),
            ),
            TextField(
              controller: _controller,
              keyboardType: TextInputType.number,
              decoration: const InputDecoration(
                //装饰
                filled: true, //填充
                fillColor: Color(0xffF3F6F9), //填充颜色
                constraints: BoxConstraints(maxHeight: 35), //约束信息
                border: UnderlineInputBorder(
                  //边线信息
                  borderSide: BorderSide.none,
                  borderRadius: BorderRadius.all(Radius.circular(6)),
                ),
                hintText: "请输入 0~99 数字", //提示字
                hintStyle: TextStyle(fontSize: 14), //提示字样式
                prefixIcon: Icon(Icons.search),
              ),
              onSubmitted: (value) {
                setState(() {
                  inputValue = int.parse(value);
                });
                if (value == _value.toString()) {
                  setState(() {
                    _guessing = true;
                  });
                }
              },
            ),
            Expanded(
              child: Container(
                  color: bgcBig,
                  child: const Align(
                    alignment: Alignment.center,
                    child: Text(
                      '大了',
                      style: TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  )),
            ),
            Expanded(
                child: Container(
                    color: smallBig,
                    child: const Align(
                      alignment: Alignment.center,
                      child: Text(
                        '小了',
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      ),
                    )))
          ]),
          Center(
            child: Text(
              valueStr,
              style: const TextStyle(fontSize: 30),
            ),
          )
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _generateRandomValue,
        tooltip: '点击生成随机数',
        child: const Icon(Icons.refresh),
      ),
    );
  }

  void onTapHistory() {
    print('点击了历史记录');
  }
}
